 // 定义添加球体的方法
 export function addConnectionSphere(viewer, position, radius, color) {
 
  viewer.entities.add({
      position: position,
      ellipsoid: {
          radii: new Cesium.Cartesian3(radius, radius, radius),
          material: Cesium.Color[color.toUpperCase()].withAlpha(0.8)
      },
      // 给球体实体打上标签
      type: 'sphere'
  });
  // console.log('Sphere added at:', position);
}

// 清除所有球体实体
export function clearAllSpheres(viewer) {
  const spheres = viewer.entities.values;
  for (const entity of spheres) {
    if (entity.type === 'sphere') {
      viewer.entities.remove(entity);
    }
  }
}
// 添加无人机模型的函数
export function addDroneModel(viewer, position, modelUrl) {
  return viewer.entities.add({
    position: position,
    model: {
      uri: modelUrl,
      minimumPixelSize: 128,
      maximumScale: 1000,
      scale: 1,
      minimumHeight: 0,
      maximumHeight: 1000,
    }
  });
}

// export function addDroneModel(viewer, position, modelUrl) {
//   // 创建一个实体并设置位置
//   const entity = viewer.entities.add({
//     position: position,
//     model: {
//       uri: modelUrl,
//       minimumPixelSize: 128, // 最小像素大小
//       maximumScale: 1000,    // 最大缩放比例
//       scale: 1,              // 初始缩放比例
//       minimumHeight: 0,      // 最小高度
//       maximumHeight: 1000,   // 最大高度
//     }
//   });

//   // 给模型实体打上标签
//   entity.type = 'drone';
// }
// export function clearAllDrones(viewer) {
//   const drones = viewer.entities.values;
//   for (const entity of drones) {
//     if (entity.type === 'drone') {
//       viewer.entities.remove(entity);
//     }
//   }
// }

// 绘制大范围矩形
export function drawLargeRectangle(viewer, corners, color, dashed) {
    var positions = corners.map(function(corner) {
      return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, 0);
    });
  
    // 添加实体到 Cesium 视图
    viewer.entities.add({
      polygon : {
        hierarchy : positions,
        material : Cesium.Color.fromCssColorString(color).withAlpha(0), // 设置颜色和透明度
        outline : true,
        outlineColor : Cesium.Color.fromCssColorString(dashed ? 'skyblue' : 'black') // 设置虚线颜色
      }
    });
  }

  // 绘制单个正方形
function drawSquare(viewer, corners, color) {
    var positions = corners.map(function(corner) {
      return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, 0);
    });
  
    // 添加实体到 Cesium 视图
    var material = color === 'transparent'
      ? Cesium.Color.fromCssColorString('rgba(0, 0, 0, 0)') // 完全透明
      : Cesium.Color.fromCssColorString(color).withAlpha(0.5); // 设置颜色和透明度
  
    viewer.entities.add({
      polygon : {
        hierarchy : positions,
        material : material,
        outline : true,
        outlineColor : Cesium.Color.SKYBLUE // 设置轮廓颜色
      }
    });
  }
  
  // 分割并随机给部分正方形上色
  export function divideAndColorSquares(viewer, largeRectangleCorners, squareSideLength) {
    var minX = Math.min(largeRectangleCorners[0].longitude, largeRectangleCorners[1].longitude);
    var maxX = Math.max(largeRectangleCorners[0].longitude, largeRectangleCorners[1].longitude);
    var minY = Math.min(largeRectangleCorners[0].latitude, largeRectangleCorners[2].latitude);
    var maxY = Math.max(largeRectangleCorners[0].latitude, largeRectangleCorners[2].latitude);
  
    for (var lat = minY; lat < maxY; lat += squareSideLength) {
      for (var lon = minX; lon < maxX; lon += squareSideLength) {
        var corners = [
          { longitude: lon, latitude: lat },
          { longitude: lon + squareSideLength, latitude: lat },
          { longitude: lon + squareSideLength, latitude: lat + squareSideLength },
          { longitude: lon, latitude: lat + squareSideLength }
        ];
  
        // 随机决定是否上色
        if (Math.random() < 0.3) { // 比如 30% 的概率上色
          var color = Math.random() < 0.5 ? 'skyblue' : 'darkblue';
          drawSquare(viewer, corners, color);
        } else {
          drawSquare(viewer, corners, 'transparent');
        }
      }
    }
  }

// 绘制大范围立体矩形
export function drawLargeRectangle3D(viewer, corners, color, dashed, height) {
  var positions = corners.map(function(corner) {
    // 确保经纬度值有效
    if (isNaN(corner.longitude) || isNaN(corner.latitude)) {
      console.warn("Invalid corner coordinates:", corner);
      return null;
    }
    // 使用 Cesium.Cartesian3.fromDegrees 直接传递经纬度和高度
    return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, 0);
  }).filter(pos => pos !== null); // 移除任何可能的 null 条目

  // 将地面位置复制一份，增加高度信息
  var extrudedPositions = corners.map(function(corner) {
    if (isNaN(corner.longitude) || isNaN(corner.latitude)) {
      console.warn("Invalid corner coordinates:", corner);
      return null;
    }
    // 设置高度
    return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, height);
  }).filter(pos => pos !== null); // 移除任何可能的 null 条目

  // 添加实体到 Cesium 视图
  viewer.entities.add({
    polygon: {
      hierarchy: new Cesium.PolygonHierarchy(positions),
      extrudedHeight: height,
      material: Cesium.Color.fromCssColorString(color).withAlpha(0), // 设置颜色和透明度
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString(dashed ? 'skyblue' : 'black'), // 设置轮廓颜色
    }
  });
}

// 绘制单个立体正方形
function drawSquare3D(viewer, corners, color, height) {
  var positions = corners.map(function(corner) {
    // 确保经纬度值有效
    if (isNaN(corner.longitude) || isNaN(corner.latitude)) {
      console.warn("Invalid corner coordinates:", corner);
      return null;
    }
    return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, 0);
  }).filter(pos => pos !== null); // 移除任何可能的 null 条目

  // 设置高度
  var extrudedPositions = corners.map(function(corner) {
    if (isNaN(corner.longitude) || isNaN(corner.latitude)) {
      console.warn("Invalid corner coordinates:", corner);
      return null;
    }
    return Cesium.Cartesian3.fromDegrees(corner.longitude, corner.latitude, height);
  }).filter(pos => pos !== null); // 移除任何可能的 null 条目

  // 添加实体到 Cesium 视图
  var material = color === 'transparent'
    ? Cesium.Color.fromCssColorString('rgba(0, 0, 0, 0)') // 完全透明
    : Cesium.Color.fromCssColorString(color).withAlpha(0.5); // 设置颜色和透明度

  viewer.entities.add({
    polygon: {
      hierarchy: new Cesium.PolygonHierarchy(positions),
      extrudedHeight: height,
      material: material,
      outline: true,
      outlineColor: Cesium.Color.SKYBLUE // 设置轮廓颜色
    }
  });
}

// 分割并随机给部分正方形上色
export function divideAndColorSquares3D(viewer, largeRectangleCorners, squareSideLength, height) {
  var minX = Math.min(largeRectangleCorners[0].longitude, largeRectangleCorners[1].longitude);
  var maxX = Math.max(largeRectangleCorners[0].longitude, largeRectangleCorners[1].longitude);
  var minY = Math.min(largeRectangleCorners[0].latitude, largeRectangleCorners[2].latitude);
  var maxY = Math.max(largeRectangleCorners[0].latitude, largeRectangleCorners[2].latitude);

  for (var lat = minY; lat < maxY; lat += squareSideLength) {
    for (var lon = minX; lon < maxX; lon += squareSideLength) {
      var corners = [
        { longitude: lon, latitude: lat },
        { longitude: lon + squareSideLength, latitude: lat },
        { longitude: lon + squareSideLength, latitude: lat + squareSideLength },
        { longitude: lon, latitude: lat + squareSideLength }
      ];

      // 随机决定是否上色
      if (Math.random() < 0.3) { // 比如 30% 的概率上色
        var color = Math.random() < 0.5 ? 'skyblue' : 'darkblue';
        drawSquare3D(viewer, corners, color, height);
      } else {
        drawSquare3D(viewer, corners, 'transparent', height);
      }
    }
  }
}
